unit MainFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, CSVParser, StdCtrls, DB, Grids, DBGrids, ExtCtrls, Buttons;

type

  TFrmMain = class(TForm)
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    LbTables: TListBox;
    DlgOpenFile: TOpenDialog;
    BbnOpen: TBitBtn;
    PnlFileName: TPanel;
    LblFileName: TLabel;
    Splitter1: TSplitter;
    procedure LbTablesClick(Sender: TObject);
    procedure BbnOpenClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    FParser : ICSVParser;
    procedure PopulateListBox;
  public
    { Public declarations }
  end;

var
  FrmMain: TFrmMain;

implementation

{$R *.dfm}

procedure TFrmMain.FormShow(Sender: TObject);
  function init_dir: string;
    var
    inx: integer;
    begin
    Result := ExtractFileDir( Application.ExeName );
    inx := Length( Result );
    if Result[ inx ] = '\' then
      dec( inx );
    repeat
      if Result[ inx ] = '\' then
        Break; //==========>
      dec( inx );
    until False;
    Result := Copy( Result, 1, inx );
    if DirectoryExists( Result + 'Data\' ) then
      Result := Result + '\Data';
    end;

  begin
  DlgOpenFile.InitialDir := init_dir;
  end;


procedure TFrmMain.LbTablesClick(Sender: TObject);
  begin
  DataSource1.DataSet := FParser.DataSet[ LbTables.ItemIndex ];
  end;

procedure TFrmMain.BbnOpenClick(Sender: TObject);
  begin
  if DlgOpenFile.Execute then
    begin
    LblFileName.Caption := DlgOpenFile.FileName;
    DataSource1.DataSet := nil;
    FParser := nil;
    FParser := TCSVParser.Create( DlgOpenFile.FileName );
    PopulateListBox;
    end;
  end;

procedure TFrmMain.PopulateListBox;
  var
  counter: integer;
  begin
  for counter := 0 to self.FParser.DataSetCount - 1 do
    LbTables.Items.Add( FParser.DataSetName[ counter ] );
  end;

end.
